﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.Threading;
using System.Xml.Linq;


namespace FlexySys.Common.Items
{

    public class DocumentData : IDocumentData
    {
        IDictionary<string, IDataItem> _data = new Dictionary<string, IDataItem>();
        public DocumentData(XElement element)
        {
            ParseElements(element);
        }

        private XElement  _Element;

        public XElement  Element
        {
            get { return _Element; }
            set {

                _Element = value;
                ParseElements(_Element);
            }
        }
        

        private void ParseElements(XElement element)
        {
            foreach (var el in element.Element("Data").Elements())
            {
                string elID = el.Attribute("ID").Value;
                object elValue = el.Element("Value").Value;
                if (!_data.ContainsKey (elID))
                {
                    _data.Add(elID, new DataItem { ID=elID ,Value =elValue ==null || elValue.ToString()=="" ? null :elValue});
                }
            }
        }




        public IDataItem  this[string key]
        {
            get
            {
                return _data.ContainsKey(key ) ? _data[key] :null ;
            }
            set
            {
                _data[key] = value;
            }
        }
    }
}
